/*
 * $Id: ProgressTextView.java 282 2008-04-10 22:36:54Z jasta $
 *
 * Copyright (C) 2008 Josh Guilfoyle <jasta@devtcg.org>
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2, or (at your option) any
 * later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 */

package org.devtcg.five.music.widget;

import java.util.Map;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.LinearGradient;
import android.graphics.Shader.TileMode;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.Transformation;
import android.widget.TextView;

public class ProgressTextView extends TextView
{
	private boolean mDraining = false;
	private long mLastDrawingTime;
	private Transformation mTransformation;
	private AlphaAnimation mAnimation;
	
	public ProgressTextView(Context context)
	{
		super(context);
	}

	public ProgressTextView(Context context, AttributeSet attrs, Map inflateParams)
	{
		super(context, attrs, inflateParams);
	}

	public ProgressTextView(Context context, AttributeSet attrs, Map inflateParams, int defStyle)
	{
		super(context, attrs, inflateParams, defStyle);
	}
	
	protected void startAnimation()
	{
		mTransformation = new Transformation();
		mAnimation = new AlphaAnimation(0.0F, 1.0F);
		mAnimation.setRepeatMode(Animation.NO_REPEAT);
		mAnimation.setInterpolator(new DecelerateInterpolator());
		mAnimation.setDuration(1200);
		mAnimation.setStartTime(-1L);

		postInvalidate();
	}
	
	protected void stopAnimation()
	{
		mAnimation = null;
		mTransformation = null;
	}
	
	protected void onDraw(Canvas canvas)
	{
		long now = getDrawingTime();

		if (mAnimation != null)
		{
			if (now < mLastDrawingTime + 150)
				return;

			mAnimation.getTransformation(now, mTransformation);
			float scale = mTransformation.getAlpha();
			setProgress((int)(scale * 100F));
		}

		super.onDraw(canvas);

		if (mAnimation != null)
		{
			postInvalidateDelayed(150L);
			mLastDrawingTime = now;
			
			if (mAnimation.isEnded() == true)
				stopAnimation();
		}
	}

	public void setProgress(int l)
	{
		int w = this.getMeasuredWidth();

		int x0 = (int)(((float)l / 100.0) * (float)w);
		int x1 = x0 + 15;

		if (x1 > w)
			x1 = w;

		int cWhite = 0xffffffff;
		int cOrange = 0xffffa239;

		int c0;
		int c1;

		if (mDraining == true)
		{
			c0 = cWhite;
			c1 = cOrange;
		}
		else
		{
			c0 = cOrange;
			c1 = cWhite;
		}

		getPaint().setShader(new LinearGradient(x0, 0, x1, 0, c0, c1, TileMode.CLAMP));

		if (mDraining != true)
			postInvalidate();
	}

	public void setVisibility(int v)
	{
		if (getVisibility() != v)
		{
			super.setVisibility(v);
			
			if (v == View.INVISIBLE || v == View.GONE)
				stopAnimation();
		}
	}
	
	public void drainProgress()
	{
		mDraining = true;
		startAnimation();
	}
}
